//
//  UIDevice+ProcessesAdditions.m
//  TESTProcessName
//
//  Created by ikuaiyue on 15/6/30.
//  Copyright (c) 2015年 ikuaiyue. All rights reserved.
//

#import "UIDevice+ProcessesAdditions.h"
#import <sys/sysctl.h>
#import "CurrentData.h"
#import "TimeModel.h"
@implementation UIDevice (ProcessesAdditions)
- (NSArray *)runningProcesses:(NSInteger)num dataList:(NSArray *)DataList {
    
    int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
    size_t miblen = 4;
    
    size_t size;
    int st = sysctl(mib, miblen, NULL, &size, NULL, 0);
    
    struct kinfo_proc * process = NULL;
    struct kinfo_proc * newprocess = NULL;
    
    do {
        
        size += size / 10;
        newprocess = realloc(process, size);
        
        if (!newprocess){
            
            if (process){
                free(process);
            }
            
            return nil;
        }
        
        process = newprocess;
        st = sysctl(mib, miblen, process, &size, NULL, 0);
        
    } while (st == -1 && errno == ENOMEM);
    
    if (st == 0){
        
        if (size % sizeof(struct kinfo_proc) == 0){
            int nprocess = size / sizeof(struct kinfo_proc);
            
            if (nprocess){
                
                NSMutableArray * array = [[NSMutableArray alloc] init];
                
                for (int i = nprocess - 1; i >= 0; i--){
                    
                    NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];
                    NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];
                    
                    NSDictionary * dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName, nil]
                                                                        forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName", nil]];
                   
                   
                    
                    if (DataList) {
                        if (DataList.count) {
                            for (NSDictionary *dics in DataList) {
//                                LOG(@"%@ ===%@",dics[@"packageName"],dict[@"ProcessName"]);
                                if ([dict[@"ProcessName"] isEqualToString:dics[@"packageName"]]) {
                                    TimeModel *timemodel = [[TimeModel alloc]init];
                                    timemodel.pid = [NSString stringWithFormat:@"%@",dict[@"ProcessID"]];
                                    timemodel.pname = dict[@"ProcessName"];
                                    
                                    NSDateFormatter *dateFormatter3 = [[NSDateFormatter alloc] init];
                                    [dateFormatter3 setDateFormat:@"yyyy-MM-dd"];
                                    timemodel.pdata = [dateFormatter3 stringFromDate:[NSDate date]];
                                    if ([CurrentData fetchAllFriendsFromLocal:timemodel.pname].count) {
                                        NSArray *arr  = [CurrentData fetchAllFriendsFromLocal:timemodel.pname];
                                        TimeModel *model = arr[0];
                                        if ([timemodel.palltime integerValue]<300) {
                                            timemodel.palltime = [NSString stringWithFormat:@"%ld",[model.palltime integerValue]+num];
                                            [CurrentData uptateModel:timemodel];
                                        }
                                        
                                        
                                    }else
                                    {
                                        timemodel.palltime = @"0";
                                        timemodel.type = @"0";
                                        [CurrentData saveData:timemodel];
                                    }
                                    
                                }
                            }

                        }
                    }
                    
                    
                }
                
                free(process);
            }
        }
    }
   
    return [CurrentData fetchAll];
}

- (NSArray *)runningProcesses:(NSInteger)num  {
     NSMutableArray * array = [[NSMutableArray alloc] init];
    int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
    size_t miblen = 4;
    
    size_t size;
    int st = sysctl(mib, miblen, NULL, &size, NULL, 0);
    
    struct kinfo_proc * process = NULL;
    struct kinfo_proc * newprocess = NULL;
    
    do {
        
        size += size / 10;
        newprocess = realloc(process, size);
        
        if (!newprocess){
            
            if (process){
                free(process);
            }
            
            return nil;
        }
        
        process = newprocess;
        st = sysctl(mib, miblen, process, &size, NULL, 0);
        
    } while (st == -1 && errno == ENOMEM);
    
    if (st == 0){
        
        if (size % sizeof(struct kinfo_proc) == 0){
            int nprocess = size / sizeof(struct kinfo_proc);
            
            if (nprocess){
                
               
                
                for (int i = nprocess - 1; i >= 0; i--){
                    
                    NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];
                    NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];
                    
                    NSDictionary * dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName, nil]
                                                                        forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName", nil]];
                   LOG(@"==%@",dict[@"ProcessName"]);
                    [array addObject:dict[@"ProcessName"]];
            
                        }

                free(process);
            }
        }
    }
    
    return array;
}

@end
